package com.project;

import java.io.*;

public class Response {
    private OutputStream out;

    public Response(OutputStream out) {

    }

    /**
     * 发送文本数据到客户端
     *
     * @param msg 发送的文本
     */
    public void sendMessage(String msg) {
        try {
            out.write(msg.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void sendFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()){
            return;
        }
        try( InputStream in=new FileInputStream(file);
                ){
            byte[] by = new byte[1024];
            int len=0;
            while ((len=in.read(by))!=-1){
                out.write(by,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
